package com.ruoyi.project.ypjxc.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.ypjxc.domain.Buy;
import com.ruoyi.project.ypjxc.domain.Drug;
import com.ruoyi.project.ypjxc.domain.Order;
import com.ruoyi.project.ypjxc.domain.Supplier;
import com.ruoyi.project.ypjxc.service.YpjxcDrugService;
import com.ruoyi.project.ypjxc.service.YpjxcOrderService;

import com.ruoyi.project.ypjxc.service.YpjxcSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ypjxc/order")
public class YpjxcOrderController extends BaseController {
    @Autowired
    private YpjxcOrderService orderService;

    @Autowired
    private YpjxcSupplierService supplierService;

    @Autowired
    private YpjxcDrugService drugService;



    /**
     * 获取所有的药品表
     * @param drug
     * @return
     */

    @PreAuthorize("@ss.hasPermi('ypjxc:drug:list')")
    @GetMapping("/drugList")
    public TableDataInfo drugList(Drug drug) {
        startPage();
        List<Drug> list = drugService.selectDrugList(drug);
        return getDataTable(list);
    }




    /**
     * 获取药品订单列表
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(Order order) {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 获取所有供应商订单列表
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:order:list')")
    @GetMapping("/getSupplier")
    public Map<String ,List<Supplier>> getSupplier() {
        List<Supplier> suppliers = supplierService.selectSupplierList(new Supplier());
        HashMap<String, List<Supplier>> map = new HashMap<>();
        map.put("data",suppliers);
        return map;
    }

    /**
     * 查询药品订单号
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:order:query')")
    @Log(title = "药品订单管理", businessType = BusinessType.INSERT)
    @GetMapping("/getOrderId")
    public Map<String, Object> getOrderId() {
        Order order=null;
        String orderId=null;
        do {
            //生成随机订单号,根据单据号查询订单信息,如果不为空则说明重复,需重新生成订单号,如果为空则返回
            Date date = new Date();
            int i= (int) (Math.random()*9000+1000);
            long time = date.getTime();
            orderId="CG"+time+i;
            order = orderService.selectOrderById(orderId);
        }while (order!=null);
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderId",orderId);
        String createBy = SecurityUtils.getUsername();
        map.put("createBy",createBy);

        return map;
    }

    /**
     * 新增药品订单
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:order:add')")
    @Log(title = "药品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Drug drug) {
        if (drug==null){
            return AjaxResult.error("信息不能为空");
        }
        drug.setCreateBy(SecurityUtils.getUsername());
        return toAjax(drugService.insertDrug(drug));
    }

    /**
     * 修改药品订单
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:order:edit')")
    @Log(title = "药品订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Order order)
    {
        if (order==null){
            return AjaxResult.error("信息不能为空");
        }
        order.setCreateBy(SecurityUtils.getUsername());
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 根据id查询
     * @param orderId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable String orderId)
    {
        return AjaxResult.success(orderService.selectOrderById(orderId));
    }

    @PreAuthorize("@ss.hasPermi('ypjxc:order:remove')")
    @Log(title = "药品订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult delete(@PathVariable Long[] orderIds){
        return toAjax(orderService.deleteOrderByIds(orderIds));
    }


    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:drug:add')")
    @Log(title = "药品管理", businessType = BusinessType.INSERT)
    @PostMapping("/addOrder")
    public AjaxResult addOrder(@Validated @RequestBody Order order) {
        if (order==null){
            return AjaxResult.error("信息不能为空");
        }
        order.setCreateBy(SecurityUtils.getUsername());
        order.setStatus("1");
        return toAjax(orderService.insertOrder(order));
    }
}
